//
// Created by Dinger on 2021/9/18.
//

#include "../Header files/lexical_form.h"

LexicalForm::LexicalForm() {
    _keyWordPosition_up = 11;
    _keyWordPosition_down = 20;
}

LexicalForm::LexicalForm(string fileName) {
    _keyWordPosition_up = 11;
    _keyWordPosition_down = 20;
    readFile(fileName);
    findKeyWord();
}

void LexicalForm::readFile(string fileName) {
    ifstream codeFile1(fileName);
    string readWord;
    if (!codeFile1.is_open()) {
        cout << "Error opening file";
        exit(1);
    }
    codeFile1 >> readWord;
    codeFile1 >> readWord;
    while (codeFile1 >> readWord) {
        string readWord2;
        codeFile1 >> readWord2;
        Word word(atoi(readWord.c_str()), readWord2);
        _lexicalForm.push_back(word);
    }
}

void LexicalForm::findKeyWord() {
    for (int i = 0; i < _lexicalForm.size(); i++) {
        if (_lexicalForm[i].getWordNumber() < _keyWordPosition_up) {
            _systemWordForm.insert(
                    pair<string, int>(_lexicalForm[i].getWordMessage(), _lexicalForm[i].getWordNumber()));
            continue;
        }
        if (_lexicalForm[i].getWordNumber() > _keyWordPosition_down) {
            _systemWordForm.insert(
                    pair<string, int>(_lexicalForm[i].getWordMessage(), _lexicalForm[i].getWordNumber()));
            continue;
        }
        _keyWordForm.insert(pair<string, int>(_lexicalForm[i].getWordMessage(), _lexicalForm[i].getWordNumber()));
    }
}